<template>
  <a-row>
    <a-col flex="auto">
      <a-menu
        mode="horizontal"
        theme="dark"
        :selected-keys="selectedKeys"
        @menu-item-click="doMenuOnclick"
      >
        <a-menu-item key="/">
          <div class="title-bar">
            <img alt="" class="logo" src="../assets/logo.svg" />
            <div class="title">Coder</div>
          </div>
        </a-menu-item>
        <a-menu-item v-for="route in visibleRoutes" :key="route.path">
          {{ route.name }}
        </a-menu-item>
      </a-menu>
    </a-col>
  </a-row>
</template>
<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title {
  font-size: medium;
}

.logo {
  height: 48px;
}
</style>
<script setup lang="ts">
import { routes } from "@/router/routes";
import { computed, ref } from "vue";
import checkAccess from "../access/checkAccess";
import { useRouter } from "vue-router";
import { useStore } from "vuex";

const router = useRouter();
const store = useStore();
const selectedKeys = ref(["/"]);
const visibleRoutes = computed(() => {
  return routes.filter((item, index) => {
    if (item.meta?.hideInMenu) {
      return false;
    }
    return checkAccess(
      store.state.user.loginUser,
      item?.meta?.access as string
    );
  });
});
//菜单点击事件
const doMenuOnclick = (key: string) => {
  router.push(key);
};
router.afterEach((to, from, failure) => {
  selectedKeys.value = [to.path];
});
</script>
